package xyz.xiezc.mzix.schedule.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;
import xyz.xiezc.mzix.common.dto.PageDTO;
import xyz.xiezc.mzix.common.dto.WebResponse;
import xyz.xiezc.mzix.schedule.dao.PageRepository;
import xyz.xiezc.mzix.schedule.entity.PageDO;
import xyz.xiezc.mzix.schedule.service.ScheduleService;

@RestController
@RequestMapping("schedule")
public class ScheduleController {

    @Autowired
    private ScheduleService scheduleService;

    @Autowired
    private PageRepository pageRepository;

    /**
     * 加入一个页面爬取请求
     */
    @GetMapping("addPage")
    public WebResponse addPage(@RequestParam String code, @RequestParam String url) {
        String timeout = scheduleService.getSession(code, "config_timeout");
        PageDTO pageDTO = new PageDTO();
        pageDTO.getRequest().setUrl(url);
        pageDTO.getRequest().setMethod("GET");
        pageDTO.getRequest().setTimeout(Convert.toLong(timeout));
        PageDO pageDO = BeanUtil.copyProperties(pageDTO, PageDO.class);
        PageDO save = pageRepository.save(pageDO);
        return WebResponse.success("page", save);
    }

    @GetMapping("schedule")
    public WebResponse  schedule(){
        scheduleService.execute();
        return WebResponse.success( );
    }
}
